package com.yuan.spring.thymeleaf;

import org.junit.Test;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import java.io.FileWriter;

/**
 * thymeleaf生成静态html
 * Created by 袁恩光 on 2017/11/21.
 */
public class HtmlTest {


    @Test
    public void ttt() throws Exception {
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        //模板所在目录，相对于当前classloader的classpath。
        resolver.setPrefix("templates/");
        //模板文件后缀
        resolver.setSuffix(".html");
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(resolver);
        //构造上下文(Model)
        Context context = new Context();
        context.setVariable("name", "蔬菜列表");
        context.setVariable("array", new String[]{"土豆", "番茄", "白菜", "芹菜"});
        //渲染模板
        FileWriter write = new FileWriter("D:/result.html");
        templateEngine.process("example", context, write);
    }


}
